package com.kanche.bigbaby.collection;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PhotoMarkerUtil {
	
//	private static Handler mHandler = new Handler();
//	public static void dealPhotoFile(final String file,final OnMarkFinished onMarkFinished){
		public static Bitmap dealPhotoFile(final String file){
		
		BufferedOutputStream bos = null;
        Bitmap icon = null;
        try  
        {  
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;  
            BitmapFactory.decodeFile(file, options); //此时返回bm为空
            float percent =  
                options.outHeight > options.outWidth ? options.outHeight / 960f : options.outWidth / 960f;  
              
            if (percent < 1)  
            {  
                percent = 1;  
            }  
            int width = (int)(options.outWidth / percent);  
            int height = (int)(options.outHeight / percent);  
            icon = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
              
            //初始化画布 绘制的图像到icon上    
            Canvas canvas = new Canvas(icon);
            //建立画笔    
            Paint photoPaint = new Paint();
            //获取跟清晰的图像采样    
            photoPaint.setDither(true);  
            //过滤一些    
            //                    photoPaint.setFilterBitmap(true);  
            options.inJustDecodeBounds = false;  
              
            Bitmap prePhoto = BitmapFactory.decodeFile(file);
            if (percent > 1)  
            {  
                prePhoto = Bitmap.createScaledBitmap(prePhoto, width, height, true);
            }  
              
            canvas.drawBitmap(prePhoto, 0, 0, photoPaint);
              
            if (prePhoto != null && !prePhoto.isRecycled())  
            {  
                prePhoto.recycle();  
                prePhoto = null;  
                System.gc();
            }  
              
//            //设置画笔
//            Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
//            //字体大小
//            textPaint.setTextSize(20.0f);
//            //采用默认的宽度
//            textPaint.setTypeface(Typeface.DEFAULT);
//            //采用的颜色
//            textPaint.setColor(Color.YELLOW);
//            //阴影设置
//            //                textPaint.setShadowLayer(3f, 1, 1, Color.DKGRAY);
//
//            // 时间水印
////            String mark = getCurrTime("yyyy-MM-dd HH:mm:ss");
//            String mark = "kanche.com";
//            float textWidth = textPaint.measureText(mark);
//            canvas.drawText(mark, width - textWidth - 10, height - 26, textPaint);
              
            bos = new BufferedOutputStream(new FileOutputStream(file));
              
            int quaility = (int)(100 / percent > 80 ? 80 : 100 / percent);  
            icon.compress(CompressFormat.JPEG, 100, bos);
            bos.flush();
            return getBitmapWithLimtedM(new File(file));

        }  
        catch (Exception e)
        {  
            e.printStackTrace();  
        }  
        finally  
        {
            if (bos != null)  
            {  
                try  
                {  
                    bos.close();  
                }  
                catch (IOException e)
                {  
                    e.printStackTrace();  
                }  
            }  
            if (icon != null && !icon.isRecycled())  
            {  
                icon.recycle();  
                icon = null;  
                System.gc();
            }  
        }
            return  null;
		
	}
	
	public interface OnMarkFinished{
		void onMarkFinished();
	}
	private static String getCurrTime(String pattern)
	{  
	    if (pattern == null)  
	    {  
	        pattern = "yyyyMMddHHmmss";  
	    }  
	    return (new SimpleDateFormat(pattern)).format(new Date());
	}

    //以最省内存的方式读取本地资源的图片
    public static Bitmap getBitmapWithLimtedM(File imgFile) throws Exception {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;

        return BitmapFactory.decodeStream(new FileInputStream(imgFile), null, opt);
    }
}
